/*
 * OREKIT-X
 * Copyright 2002-2008 CS Communication & Systemes
 * 
 * Parts of this software package have been licensed to CS
 * Communication & Systemes (CS) under one or more contributor license
 * agreements.  See the NOTICE file distributed with this work for
 * additional information.
 *  
 * This is an experimental copy of OREKIT from www.orekit.org.
 * Please use the original OREKIT from orekit.org for normal work
 * unrelated to this research project.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License.  You
 * may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package ore.attitudes;

import ore.Attitude;
import ore.AttitudeLaw;
import ore.Frame;
import ore.PositionVelocity;
import ore.Transform;
import ore.errors.OrekitException;
import ore.time.AbsoluteDate;

import org.apache.commons.math.geometry.Rotation;
import org.apache.commons.math.geometry.Vector3D;

/**
 * Spin stabilized laws are handled as wrappers for an underlying
 * non-rotating law. This underlying law is typically an instance of
 * {@link CelestialBodyPointed} with the pointing axis equal to the
 * rotation axis, but can be any attitude law.
 * 
 * Instances of this class are immutable.
 * 
 * @author Luc Maisonobe
 */
public class SpinStabilized
    extends Object
    implements ore.AttitudeLaw
{

    /**
     * Underlying non-rotating attitude law. 
     */
    public final AttitudeLaw nonRotatingLaw;
    /**
     * Start date of the rotation.
     */
    public final AbsoluteDate start;
    /**
     * Rotation axis in satellite frame.
     */
    public final Vector3D axis;
    /**
     * Spin rate in radians per seconds.
     */
    public final double rate;
    /**
     * Spin vector.
     */
    public final Vector3D spin;


    /** 
     * @param nonRotatingLaw Underlying non-rotating attitude law
     * @param start Start date of the rotation
     * @param axis Rotation axis in satellite frame
     * @param rate Spin rate in radians per seconds
     */
    public SpinStabilized(AttitudeLaw nonRotatingLaw, AbsoluteDate start, Vector3D axis, double rate){
        super();
        this.nonRotatingLaw = nonRotatingLaw;
        this.start          = start;
        this.axis           = axis;
        this.rate           = rate;
        this.spin           = new Vector3D((rate / axis.getNorm()), 
                                            axis);
    }


    /** 
     * @return Underlying non-rotating attitude law
     */
    public final AttitudeLaw getNonRotatingLaw() {
        return this.nonRotatingLaw;
    }
    /** {@inheritDoc}
     */
    public final Attitude getState(AbsoluteDate date, PositionVelocity pv, Frame frame)
        throws OrekitException
    {
        /*
         * Get attitude from underlying non-rotating law
         */
        Attitude base = this.nonRotatingLaw.getState(date, pv, frame);
        Transform baseTransform = new Transform(base.getRotation(), base.getSpin());
        /*
         * Compute spin transform due to spin from reference to current date
         */
        Transform spinInfluence =
            new Transform(new Rotation(axis, rate * date.durationFrom(start)), spin);
        /*
         * Combine the two transforms
         */
        Transform combined = new Transform(baseTransform, spinInfluence);
        /*
         * Build the attitude
         */
        return new Attitude(frame, combined.getRotation(), combined.getRotationRate());
    }
}
